<?php
/**
 * Ip 插件
 * @author 后盾向军 <houdunwangxj@gmail.com>
 */

class AdminController extends AddonController {



    public function index() {
    	
        $address = M('address')->order('id desc')->select();

        $this->assign('address',$address);
        $this->display();
    }

    /**
     * 添加屏蔽ip
     */
    public function add() {
    	if(IS_POST) {
 
    		$sheng = trim(Q('sheng'));
            if($sheng==""){
                $this->error('选择错误');
            }

            if(!$this->checksheng($sheng)){
                $this->error('已经屏蔽了全省');
            }

    		$city = Q('city');
            if($city){
                $sheng="";
            }
            $data = array('sheng'=>$sheng,'city'=>$city);
            
            $add = M('address')->data($data)->add();
    	    if($add) {
                go(U('index',array('g'=>'Addon'),''));
            }else{
                $this->error('添加失败','',1);
            }

    	} else {
    		$this->display();
    	}
    	
    }

    public function del() {
      
    	$id = Q('id');
    	$where = array('id'=>array('in',$id));
    	$info = M('address')->where($where)->del();
    	if($info) {
    		go(U('index',array('g'=>'Addon'),''));
        
    	}
    }

     public function sou() {
        $sou = Q('sou_ip');
        $where['sheng'] = array('like',"%$sou%");
        $where['city'] = array('like',"%$sou%");
        $where['_logic'] = "OR";
        $data = M('address')->where($where)->select();
        if(!$data) {
            $this->ajax("");
        }
            $html="";
        foreach ($data as $key => $d) {
            $html.="<tr>";
         
            $html.='<td><input type="checkbox" name="id[]" value="'.$d['id'].'"/></td>
            <td>'.$d['id'].'</td>
            <td>'.$d['sheng'].'</td>
            <td>'.$d['city'].'</td>
            <td><a href="'.U('del',array('g' => 'Addons','id'=>$d['id']),'').'">删除</a></td>';
            $html.="</tr>";
        }
        
        $this->ajax($html);
    }

    public function checksheng($sheng){
      
        $where = array('sheng'=>$sheng,'city'=>"");
     
        $address = M('address')->where($where)->find();
        
        if($address){
            return false;
        }else{
            return true;
        }

    }


}